package com.wc.codeforces.图论.Skipping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/10/20 17:54
 * @description https://codeforces.com/contest/2024/problem/D
 */
public class Main {
    /**
     * 思路：最短路求最小损失
     * 感想：做出了第四个题, 舒服
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 400010, M = N << 1;
    static long INF = (long) 1e16;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] a = new int[N], b = new int[N];
    static Deque<Integer> deque = new LinkedList<>();
    // dist损失最小值
    static long[] sum = new long[N], dist = new long[N];
    static boolean[] st = new boolean[N];
    static long ans = 0;
    static int n, idx = 1;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
            for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
            for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
            Arrays.fill(h, 1, n + 1, 0);
            idx = 1;
            for (int i = 1; i <= n; i++) {
                if (b[i] > i) add(i, b[i], a[i]);
            }
            for (int i = n; i > 1; i--) add(i, i - 1, 0);
            bfs();
            ans = 0;
            for (int i = 1; i <= n; i++) ans = Math.max(ans, sum[i] - dist[i]);
            out.println(ans);
        }
        out.flush();
    }

    /**
     * 双端队列只有对于 0, 1才能保证先出来的是最短的,其余都是不能确定的
     */
    static void bfs() {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        dist[1] = 0;
        deque.add(1);
        while (!deque.isEmpty()) {
            int t = deque.pollFirst();
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (w[i] == 0) deque.addFirst(j);
                    else deque.addLast(j);
                }
            }
        }
    }

    static int[] q = new int[N];

    static void spfa() {
        Arrays.fill(dist, 1, n + 1, INF);
        dist[1] = 0;
        int hh = 0, tt = 1;
        q[0] = 1;
        st[1] = true;
        while (hh != tt) {
            int t = q[hh++];
            if (hh == N) hh = 0;
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (!st[j]) {
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
    }

    /**
     * dijkstra 边权必须非负
     */
    static PriorityQueue<long[]> heap = new PriorityQueue<>(Comparator.comparingLong(o -> o[0]));
    static void dijkstra() {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        dist[1] = 0;
        heap.add(new long[]{dist[1], 1});
        while (!heap.isEmpty()) {
            int t = (int) heap.poll()[1];
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    heap.add(new long[]{dist[j], j});
                }
            }
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
